#! /usr/bin/perl

use File::Copy;
use File::stat;

if (@ARGV != 2) {
  print "Too few/many arguments.\n";
  exit -1;
}

$file = $ARGV[0];
$start_sector = $ARGV[1];

unless($file_stat = stat($file)) {
  print "Error opening $file\n";
  exit -1;
}

use integer;

if ($file_stat->size % 2048 == 0 && $file_stat->size > 0) {
  $sectors = $file_stat->size / 2048;
} else {
  $sectors = ($file_stat->size / 2048) + 1;
}

no integer;

$dest_file = "$file.sectored";

unlink($dest_file);

for ($i = 0, $skip = 0, $sector = $start_sector;
     $i < $sectors;
     $sector = $sector + 1, $skip += 2048, $i++) {
  `dd "if=$file" count=2048 bs=1 skip=$skip of=temp 2> /dev/null`;
  `/home/exobyte/crc2/a.out ./temp $sector >> "$dest_file"`;
}
